home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
ICON_8
/
MEMMON_F
/
MUTILS.C
< prev
next >
Wrap
Text File
|
1990-03-02
|
5KB
|
169 lines
/*
* mutils.c: utility functions used by memmon but not peculiar to it.
*/
#include <ctype.h>
#include "memmon.h"
/*
* chop(s, w, n, c) - break string into fields, a la awk(1).
*
* s is modified in place with '\0' replacing occurrences of separator char c.
* If c is zero, any *string of* whitespace is a single separator.
*
* w is an array of n char pointers to receive addresses of the fields.
* The return value gives the number of fields actually found;
* additional entries in w are given the address of a null string.
*/
int chop(s, w, n, c)
char *s, *w[];
int n, c;
{
int i;
if (!c) /* skip leading whitespace in whitespace mode */
while (isspace(*s))
s++;
for (i = 0; i < n && *s;) { /* while array not full and string not empty */
w[i++] = s; /* store field address */
if (c)
while (*s && *s != c) /* skip to particular separator */
s++;
else
while (*s && !isspace(*s)) /* skip to whitespace */
s++;
if (!*s) /* break at end of string */
break;
*s++ = '\0'; /* terminate field and advance */
if (!c)
while (isspace(*s)) /* skip multi spaces in c=0 mode */
s++;
}
while (i < n) /* fill rest of array with pointer to "" */
w[--n] = s;
return i; /* return count of fields found */
}
/*
* trim(s, c) -- trim string at comments character c.
*
* The string s is trimmed in place by storing a null character after the last
* "significant" character. A pointer to the first "significant" character is
* returned.
*
* If c is nonzero, it indicates a comment character; the first appearance
* of c that is not escaped by '\' indicates the start of the comments field.
* Then (in any event) trailing whitespace is skipped.
*/
char *trim(s, c)
char *s;
int c;
{
char *p;
char *index();
while (isspace(*s)) /* skip initial whitespace */
s++;
p = 0;
if (c)
for (p = s; p = index(p, c); p++)
if (p == s || p[-1] != '\\') /* look for unescaped comment */
break;
if (!p)
p = s + strlen(s); /* or find end if none */
while (p > s && isspace(p[-1])) /* find last nonwhite */
--p;
*p = '\0'; /* terminate after last nonwhite */
return s; /* and return pointer */
}
/*
* pstext(s) - write s as a PostScript string.
*/
novalue pstext(s)
char *s;
{
char c;
putchar('(');
while (c = *s++) {
if (c == '\\' || c == '(' || c == ')')
putchar('\\'); /* protect \\ \( \) */
putchar(c); /* output char */
}
putchar(')');
}
/*
* pexit(file) - issue perror() message and abort.
*/
novalue pexit(file)
char *file;
{
fprintf(stderr, "%s: ", progname);
perror(file);
exit(ErrorExit);
}
/*
* litout() - set literal output mode on stdout, iff it's a tty.
* also set 9600 baud if current speed is unreasonable.
*
* Litout is only needed by binary output formats (mmrt/mmaed, not mmps or
* mmmeta). THE CODE IS BSD SYSTEM DEPENDENT; a null routine is included
* for other systems, and a manual stty command may be needed there.
*/
#ifndef GenericBSD
novalue litout () /* not BSD -- will need to set tty modes manually */
{
}
#else /* GenericBSD */
/*
* The following kludge allows compilation under Vax 4.3BSD by an ANSI C
* compiler such as gcc. It fixes a problem in <sgtty.c>. The fix is not
* general, but it works for tty devices, and that is sufficient here.
*/
#ifdef Standard
#ifdef vax
#define IOCPARM_MASK 0x7f
#define IOC_VOID 0x20000000
#define IOC_OUT 0x40000000
#define IOC_IN 0x80000000
#define _IO(t,y) (IOC_VOID|('t'<<8)|y)
#define _IOR(t,y,z) (IOC_OUT|((sizeof(z)&IOCPARM_MASK)<<16)|('t'<<8)|y)
#define _IOW(t,y,z) (IOC_IN|((sizeof(z)&IOCPARM_MASK)<<16)|('t'<<8)|y)
#define _IOWR(t,y,z) (IOC_INOUT|((sizeof(z)&IOCPARM_MASK)<<16)|('t'<<8)|y)
#endif /* vax */
#endif /* Standard */
#include <sgtty.h>
novalue litout()
{
struct sgttyb ttyb; /* for setting tty attributes */
static int ldisc = NTTYDISC;
static int lbits = LLITOUT;
int fd;
fd = fileno(stdout);
if (!isatty(fd))
return;
if (ioctl(fd, TIOCSETD, (char *) &ldisc))
pexit("can't select new tty driver");
if (ioctl(fd, TIOCGETP, (char *) &ttyb))
pexit("can't get sgtty block");
if (ioctl(fd, TIOCLBIS, (char *) &lbits))
pexit("can't set LLITOUT");
ttyb.sg_flags &= ~(RAW + ECHO);
ttyb.sg_flags |= CRMOD;
if (ttyb.sg_ospeed < B1200) /* if speed obviously bogus, */
ttyb.sg_ispeed = ttyb.sg_ospeed = B9600; /* try 9600 as a better guess */
if (ioctl(fd, TIOCSETN, (char *) &ttyb))
pexit("can't set tty attributes");
}
#endif /* GenericBSD */